#include "communication.h"
#include "messagetype.h"
#include <QDebug>
#include <QMessageBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QPalette>
#include <QColor>
#include <QTextEdit>
#include <QLineEdit>

Communication::Communication(QTcpSocket *socket, QWidget *parent)
    : QWidget(parent), tcpSocket(socket) {
    // Set window title and fixed size
    setWindowTitle("医患沟通平台");
    setFixedSize(600, 700); // 增大窗口大小

    // Set background color
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(248, 248, 255));
    this->setPalette(palette);
    this->setAutoFillBackground(true);

    // Main layout
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    mainLayout->setSpacing(15);

    // Title label
    QLabel *label = new QLabel("<h2>医患沟通平台</h2>", this);
    label->setAlignment(Qt::AlignCenter);
    mainLayout->addWidget(label);

    // Input for question
    questionEdit = new QLineEdit(this);
    questionEdit->setPlaceholderText("请输入您的问题");
    setInputFieldStyle(questionEdit);
    mainLayout->addWidget(questionEdit);

    // Button layout for send and back
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *sendButton = createStyledButton("发送", "#4CAF50");
    QPushButton *backButton = createStyledButton("返回", "#f44336");
    buttonLayout->addWidget(sendButton);
    buttonLayout->addWidget(backButton);

    mainLayout->addLayout(buttonLayout);

    // Chat display
    chatDisplay = new QTextEdit(this);
    chatDisplay->setReadOnly(true);
    chatDisplay->setStyleSheet("padding: 10px; border: 1px solid #ccc; border-radius: 5px; background-color: #f9f9f9;");
    mainLayout->addWidget(chatDisplay);

    // Set main layout
    setLayout(mainLayout);

    // Connect buttons to slots
    connect(sendButton, &QPushButton::clicked, this, &Communication::onSendButtonClicked);
    connect(backButton, &QPushButton::clicked, this, &Communication::onBackButtonClicked);
    connect(tcpSocket, &QTcpSocket::readyRead, this, &Communication::onReadyRead);
}

// Function to style input fields
void Communication::setInputFieldStyle(QLineEdit *lineEdit) {
    lineEdit->setStyleSheet("padding: 8px; border: 1px solid #ccc; border-radius: 5px;");
    lineEdit->setFixedHeight(35);
}

// Function to create styled buttons
QPushButton* Communication::createStyledButton(const QString &text, const QString &color) {
    QPushButton *button = new QPushButton(text, this);
    button->setStyleSheet(QString("background-color: %1; color: white; border: none; border-radius: 5px; padding: 10px;").arg(color));
    button->setFixedHeight(40);
    return button;
}

void Communication::onSendButtonClicked() {
    // 获取患者输入的消息
    QString patientMessage = questionEdit->text();
    if (patientMessage.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入您想提问的问题。");
        return; // 如果输入为空，直接返回
    }
    // 显示患者发出的消息
    chatDisplay->append("您: " + patientMessage);

    // 构造通信数据包
    QMap<QString, QString> communicationData;
    communicationData.insert("type", COMMUNICATE);
    communicationData.insert("target", DOCTORMESSAGE);
    communicationData.insert("message", patientMessage);
    communicationData.insert("senderType",PATIENTMESSAGE);
    // 将数据转换为 QByteArray
    QByteArray data = mapToByteArrayCustom(communicationData);

    // 连接到服务器并发送请求
    if (tcpSocket->state() == QAbstractSocket::UnconnectedState) {
        tcpSocket->connectToHost("127.0.0.1", 12345);
    }
    if (tcpSocket->waitForConnected(30000)) { // 设置连接超时为30秒
        tcpSocket->write(data);
        tcpSocket->flush();
        questionEdit->clear();
    } else {
        chatDisplay->append("系统: 无法连接到服务器，请检查网络连接。");
    }
}

void Communication::onReadyRead()
{
    QByteArray responseData = tcpSocket->readAll();
    QMap<QString, QString> response = byteArrayToMapCustom(responseData);

    // 获取医生的回复并显示
    QString responseText = response.value("message");
    if (!responseText.isEmpty()) {
        chatDisplay->append("医生: " + responseText);
    } else {
        chatDisplay->append("系统: 医生尚未回复，请稍后重试。");
    }
}

void Communication::onBackButtonClicked() {
    this->close();
}
